<template>
  <div>
    <el-card shadow="never" class="card">

      <el-button plain @click="open1">
        商品管理介绍
      </el-button>

    </el-card>

    <!-- 条件查询 -->
    <el-card shadow="never" class="card2">

      <span>条件查询</span>
      <!-- 查询表单 -->
      <el-form ref="form" :model="form" label-width="80px" size="small" :inline="true">
        <el-form-item label="商品名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="商品货号">
          <el-input v-model="form.productSn" />
        </el-form-item>
        <el-form-item label="品牌">
          <el-select v-model="form.brandName" placeholder="品牌" value-key="id">
            <el-option v-for="item in items" :key="item.id" :label="item.name" :value="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="上架状态">
          <el-select v-model="form.publishStatus" placeholder="上架状态">
            <el-option label="下架" :value="0" />
            <el-option label="上架" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select v-model="form.verifyStatus" placeholder="审核状态">
            <el-option label="未审核" :value="0" />
            <el-option label="审核通过" :value="1" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
          <el-button type="primary" @click="empty">重置</el-button>

        </el-form-item>
      </el-form>

    </el-card>

    <!-- 表单内容区 -->

    <el-card shadow="never" class="content-card3">
      <div slot="header" class="clearfix">
        <el-button type="primary" size="small" @click="toNew">新增</el-button>
        <el-button type="primary" size="small" @click="exportExcel">导出商品列表excel文件</el-button>
      </div>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column fixed type="index" align="center" width="150" label="序号" />

        <el-table-column label="商品图片" width="120">
          <template slot-scope="scope">
            <img :src="scope.row.pic" alt="" style="width: 80px; height: 80px;">
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" width="150" prop="name">
          <template slot-scope="scope">
            <div>{{ scope.row.name }}</div>
            <div>品牌:{{ scope.row.brandName }}</div>
            <div>{{ scope.row.description }}</div>
          </template>
        </el-table-column>
        <el-table-column label="商品价格" align="center" width="150">
          <template slot-scope="scope">
            <div>原价:{{ scope.row.originalPrice }}</div>
            <div>现价:{{ scope.row.price }}</div>

          </template>
        </el-table-column>
        <el-table-column prop="productCategoryName" label="商品类别" align="center" width="150" />
        <el-table-column label="标签" align="center" width="150">
          <template slot-scope="scope">
            <div>
              最新:
              <el-switch
                v-model="scope.row.newStatus"
                align="center"
                :active-value="1"
                :inactive-value="0"
              />
              <div>
                推荐:
                <el-switch
                  v-model="scope.row.recommendStatus"
                  align="center"
                  :active-value="1"
                  :inactive-value="0"
                />
              </div>

            </div>

          </template>
        </el-table-column>
        <el-table-column label="标签2" align="center" width="150">
          <template slot-scope="scope">
            <div>
              发布:
              <el-switch
                v-model="scope.row.previewStatus"
                align="center"
                :active-value="1"
                :inactive-value="0"
              />
              <div>
                审核:
                <el-switch
                  v-model="scope.row.verifyStatus"
                  align="center"
                  :active-value="1"
                  :inactive-value="0"
                />
              </div>

            </div>

          </template>
        </el-table-column>
        <el-table-column label="sku" align="center" width="150">
          <div><a href="#">编辑sku</a></div>
        </el-table-column>
        <el-table-column prop="weight" label="重量" align="center" width="150" />
        <el-table-column prop="sort" label="排序" align="center" width="150" />

        <el-table-column align="center" label="操作" width="100">
          <template slot-scope="scope">
            <!-- @click="goEdit(scope.row)" -->
            <el-button type="text" size="small" fixed="right" @click="goEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="remove(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">

        <el-pagination
          :current-page="page.currentpage"
          :page-sizes="page.pageSizes"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>

    </el-card>

  </div>
</template>

<script>
import {
  findAllBrand

} from '@/api/marketing/index.js'

import {
  productsByPage,
  del
} from '@/api/commodity/list/index.js'
export default {
  data() {
    return {
      searchForm: {},
      // 分页对象
      page: {
        currentpage: 1,
        pageSizes: [10, 15, 20, 25],
        pageSize: 10,
        total: 0
      },
      form: {
        name: '',
        productSn: '',
        brandName: '',
        publishStatus: '',
        verifyStatus: ''
      },
      items: [],
      tableData: []
    }
  },
  created() {
    // 获取商品列表
    this.getCommodiity()
    // 获取品牌列表
    findAllBrand().then(res => {
      console.log(res)
      this.items = res.data.items
    })
  },
  methods: {
    // 导出excel
    exportExcel() {
      const data = []
      const header = ['商品名称', '商品价格', '商品类别', '是否最新']
      const header_key = ['name', 'originalPrice', 'productCategoryName', 'newStatus']
      this.tableData.forEach(item => {
        var arr = []
        for (const key in header_key) {
          var itemKey = header_key[key]
          // 如果是状态,需要进行判断
          if (key === '3') {
            arr.push(item[itemKey] === 1 ? '是' : '否')
          } else {
            arr.push(item[itemKey])
          }
        }
        data.push(arr)
      })
      console.log(data)
      import('@/vendor/Export2Excel').then(excel => {
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: '商品列表', // 非必填
          autoWidth: true, // 非必填
          bookType: 'xlsx' // 非必填
        })
      })
    },
    // 编辑
    goEdit(val) {
      console.log(val)
      this.$router.push({
        name: 'EddCommodity',
        params: {
          val
        }
      })
    },
    // 去往新增
    toNew() {
      this.$router.push({
        name: 'AddCommodity'
      })
    },
    // 点击查询
    search() {
      console.log(this.form)

      // 分页初始化
      this.page = this.$options.data().page
      // 遍历表单把需要查询的数据放到searchForm里
      for (const key in this.form) {
        this.searchForm[key] = this.form[key]
      }
      this.getCommodiity()
    },
    // 删除
    remove(val) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(123)
        del(val.id).then(res => {
          console.log(res)
          if (res.success) {
            this.$message.success('删除成功')
            this.getCommodiity()
          } else {
            this.$message.error('删除失败')
          }
        })
      })
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getCommodiity()
    },
    handleCurrentChange(val) {
      this.page.currentpage = val
      this.getCommodiity()
    },
    // 商品管理介绍
    open1() {
      const h = this.$createElement

      this.$notify({
        title: '商品管理介绍',
        message: h('i', {
          style: 'color: teal'
        }, '商品管理模块可以对客户端的商品进行增删改查等相关操作,来控制客户端商品的列表展示和商品详情')
      })
    },
    // 重置
    empty() {
      this.form = this.$options.data().form
    },
    // 获取商品列表
    getCommodiity() {
      productsByPage(this.page.currentpage, this.page.pageSize, this.searchForm).then(res => {
        console.log(res)
        this.tableData = res.data.rows
        this.page.total = res.data.total
      })
    }
  }
}
</script>

<style scoped="scoped" lang="scss">
	.card2 {

		margin: 30px;
	}
</style>
